package com.quarkus.security.jdbc.controller;

import com.quarkus.security.jdbc.entity.User;
import com.quarkus.security.jdbc.service.UserService;
import org.jboss.logging.Logger;

import javax.annotation.security.PermitAll;
import javax.annotation.security.RolesAllowed;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.SecurityContext;
import java.util.List;


@Path("/user")
@ApplicationScoped
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class UserController {

    private static final Logger LOGGER = Logger.getLogger(UserController.class.getName());

    @Inject
    private UserService userService;

    @GET
    @Path("/api/public")
    @PermitAll
    public List<User> publicResource() {
        LOGGER.info("public");
        return userService.getAllProject();
    }

    @GET
    @Path("/api/admin")
    @RolesAllowed("admin")
    public String adminResource() {
        LOGGER.info("admin");
        return userService.getProjectInform();
    }

    @GET
    @Path("/api/users/user")
    @RolesAllowed("user")
    public String userResource(@Context SecurityContext securityContext) {
        LOGGER.info(securityContext.getUserPrincipal().getName());
        return userService.getProjectInform();
    }
}
